<template>
  <div class="login-container">
    <el-form class="user" :model="user" size="large">
      <div class="title-container">
        <h3 class="title" style="color: aliceblue">登录</h3>
      </div>
      <el-form-item prop="username">
        <el-input v-model="user.username" placeholder="请输入用户名" name="username" type="text" :prefix-icon="User"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="user.password" placeholder="请输入密码" name="password" type="password" :prefix-icon="Lock"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  type="primary" @click="handleLogin" class="login_button">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script setup lang="js">

import {reactive} from "vue"
import api from "@/api/index.js"
import {useLoginStore} from "@/stores/loginStore.js"
// import {ElMessage} from "element-plus";
import router from "@/router/index.js"
import {User,Lock} from "@element-plus/icons-vue";


const loginStore = useLoginStore()

const user = reactive({
  username: '',
  password: ''
})

const handleLogin = () => {

  api.getlogin({
    username: user.username,
    password: user.password
  }).then(res => {
      console.log(res)
      console.log(res.data)

    if (res.data.status === 200) {
      console.log(res.data)

      loginStore.username = res.data.username
      loginStore.password = res.data.password
      loginStore.status = res.data.status
      loginStore.role = res.data.role
      loginStore.token = res.data.token
      // handleUserRole();登录页面只做登录
      ElMessage({
        message: "登录成功",
        type: 'success',
      })
      router.push('/Homepage')
    } else if (res.data.status === 401) {
      console.log(res.data)
      ElMessage.error(res.data.error)
    }
    else {
      ElMessage.error(res.data.error)
    }
  })
}

</script>

<style scoped>
.login-container {
  width: 100%;
  height: 100%;
  background: url("../../assets/c12bf11d4ef56.jpeg");
  background-size: cover;
}

.user {
  position: relative;
  width: 400px;
  padding: 160px 35px 0;
  margin: 0 auto;
}

.title {
  font-size: 30px;
  color: aliceblue;
  text-align: center;
  margin: 20px;
}
.login_button{
  padding: 12px 30px;
  width: 100%;
  background: #4facfe;
  font-size: 1.2rem;
  border-radius: 50px;
  transition: all 0.3s ease;

}
.login_button:hover{
  background: #00a1ff;
  transform: translateY(-3px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}

</style>


